window.ali = {
	// 初始化
	init : function(){
		//console.log(this);
		// 检测web存储，如果不存在就创建
		//if(!localStorage.getItem("orders"))localStorage.setItem("orders", new Array());
		// 清空
		localStorage.clear();
		console.log("清空localStorage完毕");
	},

	// 刷新浏览器
	reload : function(){
		window.location.reload();
	},

	// 检查并推送新的订单
	checkPush : function(){
		// 解析页面的订单
		var tab = document.getElementById('tradeRecordsIndex');
		if(!tab){
			console.log('请登录PC网页支付宝，并打开“交易记录”页面');
			return;
		}
		console.log('检测到“交易记录”页面，开始执行……');
		var trs = tab.getElementsByTagName('tbody')[0].getElementsByTagName('tr');
		for (var i = 0; i<trs.length; i++) {
			var tr = trs[i];
			var tds = tr.getElementsByTagName('td');
			// 0-头像

			// 1-时间
			var date = tds[1].children[0].textContent.replace(/\s+/g,"");
			var time = tds[1].children[1].textContent.replace(/\s+/g,"");
			//console.log(date + ' ' + time);

			// 2-摘要、流水号
			var remark = tds[2].children[0].textContent.replace(/\s+/g,"");
			// 可能会有两个超链接，取第二个就是流水号的链接
			var links = tds[2].getElementsByTagName('a');
			var aliNo = links[links.length-1].getAttribute('title');

			// 3-金额
			var amount = tds[3].children[0].textContent.replace(/\s+/g,"");

			//console.log('摘要:' + remark + ',流水号:' + aliNo + ',金额:' + amount);
			// 订单数据存放到web存储
			var order = {
				datetime : date + ' ' + time,
				remark : remark,
				aliNo : aliNo,
				amount : amount,
				status : 0
			};
			var oldOrder = localStorage.getItem(aliNo);
			if(!oldOrder){
				order = JSON.stringify(order);
				localStorage.setItem(aliNo, order);
				console.log(order);
				order = encodeURI(order);
				// jsonp发送数据
				var script = document.createElement('script');
				script.setAttribute("type","text/javascript");
				script.src = "https://localhost:8443/notify/aliNotify?callback=success&json=" + order;
				document.body.appendChild(script);
			}
		}
	}
};
window.onload = function(){
	// 初始化
	ali.init();
	ali.checkPush();
}
